<?xml version="1.0" encoding="UTF-8"?>
<!--

       Copyright 2006-2018 the original author or authors.

       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.

-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>The &lt;generatedKey&gt; Element</title>
  <link rel="stylesheet" type="text/css" href="../mbgstyle.css" />
</head>
<body>
<h1>The &lt;generatedKey&gt; Element</h1>
<p>The &lt;generatedKey&gt; element is used to specify properties for
auto generated keys (from identity field or sequences).  If you specify this
element, MyBatis Generator (MBG) will generate an appropriate &lt;selectKey&gt; element inside the
generated &lt;insert&gt; element in the SQL map.  This element is an optional child
element of the <a href="table.html">&lt;table&gt;</a> element.</p>

<h2>Required Attributes</h2>
<table border="1" cellspacing="0" cellpadding="5">
  <tr>
    <th>Attribute</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>column</td>
    <td>The column name of the generated column.</td>
  </tr>
  <tr>
    <td valign="top">sqlStatement</td>
    <td>The SQL statement that will return the new value.  If this is
        an identity column, then you can use one of the predefined
        special values, or substitute the proper statement for your database.
        The predefined special values are as follows:
          <table>
            <tr>
              <th>Cloudscape</th>
              <td>This will translate to: <code>VALUES IDENTITY_VAL_LOCAL()</code></td>
            </tr>
            <tr>
              <th>DB2</th>
              <td>This will translate to: <code>VALUES IDENTITY_VAL_LOCAL()</code></td>
            </tr>
            <tr>
              <th valign="top">DB2_MF</th>
              <td>This will translate to: <br/> <code>SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1</code>
                <p>Use this value for DB2 on zOS (Main Frames) and, in some cases, iSeries (AS/400)</p>
              </td>
            </tr>
            <tr>
              <th>Derby</th>
              <td>This will translate to: <code>VALUES IDENTITY_VAL_LOCAL()</code></td>
            </tr>
            <tr>
              <th>HSQLDB</th>
              <td>This will translate to: <code>CALL IDENTITY()</code></td>
            </tr>
            <tr>
              <th valign="top">Informix</th>
              <td>This will translate to: <code>select dbinfo('sqlca.sqlerrd1') from systables where tabid=1</code></td>
            </tr>
            <tr>
              <th>MySql</th>
              <td>This will translate to: <code>SELECT LAST_INSERT_ID()</code></td>
            </tr>
            <tr>
              <th>SqlServer</th>
              <td>This will translate to: <code>SELECT SCOPE_IDENTITY()</code></td>
            </tr>
            <tr>
              <th>SYBASE</th>
              <td>This will translate to: <code>SELECT @@IDENTITY</code></td>
            </tr>
            <tr>
              <th valign="top">JDBC</th>
              <td>This will configure MBG to generate code for MyBatis3 support of JDBC
                  standard generated keys.  This is a database independent method of obtaining
                  the value from identity columns.
              </td>
            </tr>
          </table>
    </td>
  </tr>
</table>

<h2>Optional Attributes</h2>
<table border="1" cellspacing="0" cellpadding="5">
  <tr>
    <th>Attribute</th>
    <th>Description</th>
  </tr>
  <tr>
    <td valign="top">identity</td>
    <td>If <code>true</code>, then the column is flagged as an identity column and the
        generated <code>&lt;selectKey&gt;</code> element will be placed
        after the insert (for an identity column).  If <code>false</code>, then
        the generated <code>&lt;selectKey&gt;</code> will be placed before the insert
        (typically for a sequence).
        <p><b>Important:</b> Even if you specify the <code>type</code> attribute as "post",
           you should still specify this value as "true" for identity columns.  This will flag
           MBG to remove the column from the insert list.</p>
        <p>The default is <i>false</i>.</p></td>
  </tr>
  <tr>
    <td valign="top">type</td>
    <td>If specified, then this value will be added as the type of the generated <code>&lt;selectKey&gt;</code> element.
        The value of this property should be either "pre" or "post".
        <p><b>Important:</b> if a value is specified for this attribute, then the generated
        <code>&lt;selectKey&gt;</code> element will always be placed before the insert statement.</p></td>
  </tr>
</table>

<h2>Child Elements</h2>
<p>None</p>

</body>
</html>
